__author__ = "Chengye zhao (chengyezhao@gmail.com)"

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from product import Product, getProductCategory
from google.appengine.api import urlfetch
import logging

class ProcessProductJob(webapp.RequestHandler):
	def get(self):
		q = db.GqlQuery("SELECT * from Product where category = null")
		result = q.fetch(10)
		self.response.out.write("<pre>")
		for product in result:
			category = getProductCategory(product.title.encode('utf-8'))
			if category:
				self.response.out.write("Category %s guess %s title %s \n" % (str(product.category), str(category), product.title))
			else:
				self.response.out.write("Category %s guess None title %s \n" % (str(product.category), product.title))
			product.category = category
			product.put()
		self.response.out.write("</pre>")

application = webapp.WSGIApplication(
	[('/process_product', ProcessProductJob)],
	debug=True)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()
